package task2;

import java.util.ArrayList;
import java.util.Scanner;

public class test {

    static ArrayList<MyEntry> list;//动态数组
    static Scanner input=new Scanner(System.in);
    static MyEntry head;//链表头节点
    static int flag;//数字m

    public static void main(String[] args) {

        int n=input.nextInt();
        list=new ArrayList<>();
        input.nextLine();//清空输入缓存区缓存
        for(int i=0;i<n;i++){
           String[] line=input.nextLine().split(",");
           int eid=Integer.parseInt(line[0]);
           String value=line[1];
           int pas=Integer.parseInt(line[2]);
           list.add(new MyEntry(eid,value,pas));
        }
        flag= input.nextInt();
        createNodes(list.size());
    }

    private static void createNodes(int size) {
        MyEntry next;
        MyEntry last;

        head= list.get(0);
        last=head;

        for(int i=1;i<size;i++){//初始化循环链表
            next=list.get(i);
            next.last=last;
            last.next=next;
            last=next;
        }
        head.last=last;
        last.next=head;//头尾相连

        play(head);
    }

    private static void play(MyEntry entry){//遍历链表
        int count=1;
        MyEntry currentEntry=entry;

        while (list.size()>0){
            while (count<flag){
                currentEntry=currentEntry.next;
                count++;
            }
            if(count==flag){//报数为m
                currentEntry.last.next=currentEntry.next;//出列
                currentEntry.next.last=currentEntry.last;
                list.remove(currentEntry);//动态数组移除该元素
                flag=currentEntry.pas;//更新出列m
                System.out.println(currentEntry);//输出
                currentEntry=currentEntry.next;//从后一位顺时针计数
                count=1;//重置计数
            }
        }
    }

}
class MyEntry{//节点类

    MyEntry next;
    MyEntry last;
    int eid;
    String value;
    int pas;


    public MyEntry(int eid, String value, int pas) {
        this.eid=eid;
        this.value=value;
        this.pas=pas;
    }

    public String toString(){
        return eid+","+value+","+pas;
    }
}